package com.umeng.kaoti2.model;

import com.example.mvplibrary.mvplibrary.model.BaseModel;
import com.example.mylibrary.callback.BaseCallBack;
import com.example.mylibrary.client.HttpClient;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.umeng.kaoti2.callback.HttpCallBack;
import com.umeng.kaoti2.data.HighOpinionBean;
import com.umeng.kaoti2.presenter.MainPresenter;

import java.util.HashMap;
import java.util.List;

public class MainModel implements BaseModel {
    public void getData(final MainPresenter mainPresenter, LifecycleProvider lifecycleProvider) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("cityid",1);
        map.put("operaVersion","5.1.1");

        new HttpClient.Builder()
                .setBaseUrl("https://api.zhuge.com/home/v1/Appindex/")
                .setApiUrl("showHighOpinion")
                .setParamser(map)
                .setLifecycleProvider(lifecycleProvider)
                .post()
                .build()
                .excute(new HttpCallBack<List<HighOpinionBean.ListBean>>() {

                    @Override
                    protected void error(String error, int code) {
                        mainPresenter.OnError(error,code);
                    }

                    @Override
                    protected void onSucess(List<HighOpinionBean.ListBean> listBeans) {
                        mainPresenter.OnSuccess(listBeans);
                    }

                    @Override
                    public List<HighOpinionBean.ListBean> convert(JsonElement jsonElement) {
                        HighOpinionBean highOpinionBean = new Gson().fromJson(jsonElement, HighOpinionBean.class);
                        return  highOpinionBean.getList();
                    }
                });


    }
}
